// -----------------------------------------------------------------------------
// This source file is part of Matrix Platform
// 	(Universal .NET Software Development Platform)
// For the latest info, see http://www.matrixplatform.com
// 
// Copyright (c) 2009-2010, Ingenious Ltd
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// -----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Matrix.Common.FrontEnd.Ranged
{
    public partial class RangedControl : UserControl
    {
        public int Start
        {
            get { return this.trackBarStart.Value;  }
            set { this.trackBarStart.Value = value; }
        }

        public int End
        {
            get { return this.trackBarEnd.Value; }
            set { this.trackBarEnd.Value = value; }
        }

        public int Count
        {
            get { return this.trackBarEnd.Value - this.trackBarStart.Value;  }
            set { this.trackBarEnd.Value = this.trackBarStart.Value + value;  }
        }

        public int Max
        {
            get { return this.trackBarStart.Maximum; }
            set 
            {
                this.trackBarStart.Maximum = value;
                this.trackBarEnd.Maximum = value;
            }
        }

        IRanged _rangedObject = null;
        public IRanged RangedObject
        {
            get { return _rangedObject; }
            set 
            { 
                _rangedObject = value;
                UIApplyRangedObject(_rangedObject);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public RangedControl()
        {
            InitializeComponent();
        }

        protected void UIApplyRangedObject(IRanged ranged)
        {
            this.trackBarStart.Enabled = (ranged != null);
            this.trackBarEnd.Enabled = (ranged != null);

            if (ranged != null)
            {
                this.trackBarStart.Maximum = ranged.MaxRange;
                this.trackBarEnd.Maximum = ranged.MaxRange;

                this.trackBarStart.Value = ranged.StartIndex;
                this.trackBarEnd.Value = ranged.StartIndex + ranged.Count;
            }
        }

        private void trackBar_ValueChanged(object sender, EventArgs e)
        {
            this.numericUpDownStart.Value = this.trackBarStart.Value;
            this.numericUpDownEnd.Value = this.trackBarEnd.Value;

            this.labelCount.Text = "Count : " + this.Count;

            if (_rangedObject != null)
            {
                _rangedObject.StartIndex = this.trackBarStart.Value;
                _rangedObject.Count = this.trackBarEnd.Value - this.trackBarStart.Value;
            }
        }

    }
}
